/**
 * Created by Administrator on 2017/4/13.
 */
import Vue from 'vue'
import * as publicMethod from './util'
let moment = require('moment');
import {imgBaseUrl} from 'config/env.js'

//手机号转义 13400001234 => 134****1234
Vue.filter('secrecyPhone', function (value) {
  if (value && value.length == 11) {
    return `${value.substr(0, 3)}****${value.substr(7)}`
  }
  return value
})

// 性别过滤
Vue.filter('genderTrans', function (value) {
  return value && (value = value == "1" ? "男" : "女");
})
Vue.filter('petGender', function (value) {
  return value && (value = value == "1" ? "公" : "母");
})

//宠物类别过滤
Vue.filter('petClasses', function (value) {
  let msg = '';
  switch (value){
    case 'cat':
      msg = '猫猫';
      break;
    case 'dog':
      msg = "狗狗";
      break;
    default:
      msg = '猫猫';
      break;
  }
  return msg;
})

//出生日期过滤
Vue.filter('birthdayTrans', function (value) {
  return value && (moment(value).format('YYYY-MM-DD'));
})
// 详细时间过滤
Vue.filter('detailTimeTrans', function (value) {
  let msg = `${value.slice(0,4)}-${value.slice(4,6)}-${value.slice(6,8)} ${value.slice(8,10)}:${value.slice(10,12)}`;
  return msg;
})
//年龄过滤 yyyyMMdd -> 23岁
Vue.filter('ageTrans', function (value) {
  return `${moment().year() - moment(value).year()}岁`
})
//金额格式化(保留两位小数)
Vue.filter('formatAmount', function (value) {
  return value && (value.toFixed(2))
})

// 2018040210301122
function getMomentObj(time) {
  return new moment(time, 'YYYYMMDDHHmmSS')
}

function parseWeek(day) {
  var week = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"]
  let index = day.indexOf('(') + 1
  return day.substring(0, index) + week[day.charAt(index)] + day.substring(index + 1)
}



// 日期格式转换 - 4月22日（周日）10:00
Vue.filter('monthDateSingle',  val => {
  try {
    let moment = getMomentObj(val);
    let res = moment.format('M月DD日(d)H:mm')
    return parseWeek(res)
  }catch(exp) {
    console.log(' filter - dateParse error ' + val)
  }
  return val

})

// 2018-04-23
Vue.filter('YYYY-MM-DD',  val => {
  try {
    if(val.includes('选择')) return '请选择'
    let moment = getMomentObj(val);
    return moment.format('YYYY-MM-DD')
  }catch(exp) {
    console.log(' filter - dateParse error ' + val)
  }
  return val

})

// 2018.04.23
Vue.filter('YYYY.MM.DD',  val => {
  try {
    let moment = getMomentObj(val);
    return moment.format('YYYY.MM.DD')
  }catch(exp) {
    console.log(' filter - dateParse error ' + val)
  }
  return val

})

/**
 * val:
 * 2018年10月11日（周日）
 */

Vue.filter('yearMonthWeek',  val => {
  try {
    let momentDate = new moment(val, 'YYYYMMDD')
    let parse = momentDate.format('YYYY年MM月DD日(d)')
    return parseWeek(parse)
  }catch(exp) {
    console.log(' filter - dateParse error ' + val)
  }
  return val

})
Vue.filter('monthWeek',  val => {
  try {
    let momentDate = new moment(val, 'YYYYMMDD')
    let parse = momentDate.format('MM月DD日(d)')
    return parseWeek(parse)
  }catch(exp) {
    console.log(' filter - dateParse error ' + val)
  }
  return val

})


/**
 * val: {timeStart}_{timeEnd}
 * 时间范围同一天： 10月11日（周日）11:00~12:00
 */
Vue.filter('monthDateMulti',  val => {
  try {
    let momentStart = getMomentObj(val.split('_')[0])
    let momentEnd = getMomentObj(val.split('_')[1])
    let res = `${momentStart.format('M月DD日(d) H:mm')}-${momentEnd.format('H:mm')}`
    return parseWeek(res)
  }catch(exp) {
    console.log(' filter - dateParse error ' + val)
  }
  return val
})


/**
 * val: {timeStart}_{timeEnd}
 * 时间范围同一天： 2018年10月11日（周日）11:00~12:00
 */
Vue.filter('yearDateMulti',  val => {
  try {
    let momentStart = getMomentObj(val.split('_')[0])
    let momentEnd = getMomentObj(val.split('_')[1])
    let res = `${momentStart.format('YYYY年MM月DD日(d) H:mm')}-${momentEnd.format('H:mm')}`
    return parseWeek(res)
  }catch(exp) {
    console.log(' filter - dateParse error ' + val)
  }
  return val
})

/**
 * val: {time}
 */
Vue.filter('yearDateSingle',  val => {
  try {
    let momentStart = getMomentObj(val)
    let res = momentStart.format('YYYY年MM月DD日(d) H:mm')
    return parseWeek(res)
  }catch(exp) {
    console.log(' filter - dateParse error ' + val)
  }
  return val
})


/**
 * val: {time}
 * return： 23
 */
Vue.filter('day',  val => {
  try {
    let momentStart =  new moment(val, 'YYYYMMDD')
    return momentStart.format('D')
  }catch(exp) {
    console.log(' filter - dateParse error ' + val)
  }
  return val
})

/**
 * return 5月
 */
Vue.filter('month',  val => {
  try {
    let momentStart =  new moment(val, 'YYYYMM')
    let month = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二']
    let monthIndex = momentStart.format('M') - 1
    return `${month[monthIndex]}月`
  }catch(exp) {
    console.log(' filter - dateParse error ' + val)
  }
  return val
})

/**
 * 2018.10.11 14.:23:23
 */
Vue.filter('dateDotHour',  val => {
  try {
    let momentStart =  getMomentObj(val)
    return momentStart.format('YYYY.MM.DD HH:mm:SS')
  }catch(exp) {
    console.log(' filter - dateParse error ' + val)
  }
  return val
})


/**
 * return 2018.10.11
 */
Vue.filter('dateDot',  val => {
  try {
    let momentStart =  getMomentObj(val)
    return momentStart.format('YYYY.MM.DD')
  }catch(exp) {
    console.log(' filter - dateParse error ' + val)
  }
  return val
})

/**
 * 2018.10.11 14.:23:23
 */
Vue.filter('dateDotWeek',  val => {
  try {
    let momentStart =  getMomentObj(val)

    let res = momentStart.format('YYYY.MM.DD(d)')
    return parseWeek(res)
  }catch(exp) {
    console.log(' filter - dateParse error ' + val)
  }
  return val
})

/**
 * 2018.10.11 14.:23:23
 */
Vue.filter('dateDotWeekHour',  val => {
  try {
    let momentStart =  getMomentObj(val)

    let res = momentStart.format('YYYY.MM.DD(d) HH:mm:SS')
    return parseWeek(res)
  }catch(exp) {
    console.log(' filter - dateParse error ' + val)
  }
  return val
})

/**
 * 服务项目类型过滤
 * **/
Vue.filter('serviceType', val => {
  let msg = '';
  switch (val) {
    case 'foster':
      msg = '寄养';
      break;
    case 'care':
      msg = '洗护';
      break;
    case 'cosmetology':
      msg = '美容';
      break;
    default:
      break;
  }
  return msg;
})
